package com.lagou.work02.service.impl;

import com.lagou.work02.dao.AccountDao;
import com.lagou.work02.pojo.MoneyInfo;
import com.lagou.work02.service.TransferService;
import com.lagou.work02.stereotype.MyAutowired;
import com.lagou.work02.stereotype.MyService;
import com.lagou.work02.stereotype.MyTransactional;
import org.springframework.stereotype.Service;

@MyTransactional
@MyService("transferService")
public class TransferServiceImpl implements TransferService {

    @MyAutowired
    private AccountDao accountDao;

    @MyAutowired
    public void setAccountDao (AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void transfer(String fromName, String toName, int money) throws Exception {
        MoneyInfo fromInfo = accountDao.findByName(fromName);
        MoneyInfo toInfo = accountDao.findByName(toName);
        int fromMoney = fromInfo.getMoney() - money;
        int toMoney = toInfo.getMoney() + money;
        int fromResult = accountDao.updateMoneyByName(fromName, fromMoney);
        int i = 1/0;
        int toResult = accountDao.updateMoneyByName(toName,toMoney);
    }
}
